<?php

namespace App\Jobs;

use App\Models\BooksAutoUpdate;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Models\Books;
use Illuminate\Support\Facades\Log;

class ChapterUpdateQueue implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $data;

    /**
     * Create a new job instance.
     * @param $data
     * @return void
     */
    public function __construct($data = [])
    {
        //
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $data = $this->data;
        if (empty($data['id'])) {
            echo "param error";
            return;
        }
        $data = Books::select(['id', 'name'])->find($data['id']);
        if ($data->count()) {
            BooksAutoUpdate::firstOrCreate([
                'book_id' => $data['id'],
                'book_name' => $data['name'],
                'is_update' => 0,
            ]);
        }
    }

    public function failed()
    {
//        Log::info('failed');
    }
}
